<?php
/**
 *  * 系统-受国家计算机软件著作权保护 - !
 * =========================================================
 * Copy right 2018-2025 成都海之心科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: http://www.ohyu.cn
 * 这不是一个自由软件！在未得到官方有效许可的前提下禁止对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 * User: ohyueo
 * Date: 2021/5/17
 * Time: 10:49
 */

namespace app\admin\controller;
use think\facade\View;
use think\facade\Db;
use app\common\Qiniu;
use app\common\TencentCloud;

class Index extends Common
{
    public function __construct()
    {
        parent::initialize();
    }

    public function index(){
        $wherex=$where=[];
        $username = session('escort_admin_name');
        $escort_admin_merid=session('escort_admin_merid');
        if($escort_admin_merid){
            $wherex['merid']=$escort_admin_merid;
            // $where[]=['id','<>',157];//这里不等于是 不想给不是总后台管理员的人看到 
            // $where[]=['id','<>',148];
            // $where[]=['id','<>',40];
            // $where[]=['id','<>',70];
        }
        $admin = Db::name('escort_admin')->where('username', $username)->where($wherex)->field('id, name')->find();
        if(!$admin){
            return redirect('/admin/Login/index?st=2')->send();
        }
        //查看该用户的角色
        $role = Db::name('escort_admin_role')->alias('r')->join('escort_admin_role_permission p', 'r.id = p.role_id', 'LEFT')->join('escort_admin_user_role u', 'u.role_id = r.id', 'LEFT')->field('r.name, p.permission_id')->where('u.admin_id', $admin['id'])->find();

        //获取侧边栏菜单
        //一级菜单
        $top = Db::name('escort_admin_permission')->where($where)->where('is_nav', 1)->where('parent_id', 0)->where('Id', 'in', $role['permission_id'])->order('p_id asc,id asc')->select();
        //二级菜单
        $sec = Db::name('escort_admin_permission')->where($where)->where('is_nav', 2)->where('parent_id', '<>', 0)->where('Id', 'in', $role['permission_id'])->order('p_id asc,id asc')->select();
        $menu = Db::name('escort_admin_permission')->where($where)->where('is_nav', 1)->where('parent_id', '<>', 0)->where('Id', 'in', $role['permission_id'])->order('p_id asc,id asc')->select();

        View::assign('sec', $sec);
        View::assign('top', $top);
        View::assign('menu', $menu);
        View::assign('admin', $admin);

        $quan=1;//$this->isquan();
        View::assign('quan',$quan);
        $merid=getAdminMerid();
        View::assign('merid',$merid);
        return View::fetch();
    }
    public function upload(){
        //验证码token end
        $result=[];
        $type=input('param.type');
        $files = request()->file();
        foreach($files as $file) {
            $isse=check_illegal($file);
            if(!$isse){
                $result = [
                    'code' => -1,
                    'msg'  => '上传异常'
                ];
                return $result;exit;
            }
        }
        try {
            validate(['file' => [
                'fileSize' => 3145728,
                'fileExt' => 'jpg,png,gif,jpeg,mp4',
                'fileMime' => 'image/jpeg,image/png,image/gif,video/mp4',
            ]])->check(['file' => $files]);
            $cloud_type=config('-cloudsite.cloud_type')??0;//云存储类型  0本地 1七牛 2腾讯云
            if($cloud_type==2){
                //腾讯云储存
                $file=$files['file']??$files['edit'];
                // 图片存储在本地的临时路经
                $filePath = $file->getRealPath();
                // 获取图片后缀
                $ext = $file->getOriginalExtension();
                // 上传到腾讯云后保存的新图片名
                $tim=date('Ymd');
                $newImageName  =   'order/'.$tim.'/'.substr(md5($file->getOriginalName()),0,6)
                    .  rand(00000,99999) . '.'.$ext;
                TencentCloud::upload($newImageName,$filePath);
                $domain=config('-cloudsite.domain');
                //如果domain后面没有/则加上
                if(substr($domain,-1)!='/'){
                    $domain.='/';
                }
                $result = [
                    'code'     => 0,
                    'msg'      => '上传成功',
                    'data' => $domain.$newImageName,
                    'img' => $domain.$newImageName,
                    'filename' => $newImageName
                ];
                if($type){
                    $result['filename']=$domain.$newImageName;
                }
            }else if($cloud_type==1){
                //七牛云地址
                $file=$files['file'];
                // 图片存储在本地的临时路经
                $filePath = $file->getRealPath();
                // 获取图片后缀
                $ext = $file->getOriginalExtension();
                // 上传到七牛后保存的新图片名
                $tim=date('Ymd');
                $newImageName  =   'order/'.$tim.'/'.substr(md5($file->getOriginalName()),0,6)
                    .  rand(00000,99999) . '.'.$ext;
                Qiniu::upload($newImageName,$filePath);
                $domain=config('-cloudsite.domain');
                //如果domain后面没有/则加上
                if(substr($domain,-1)!='/'){
                    $domain.='/';
                }
                $result = [
                    'code'     => 0,
                    'msg'      => '上传成功',
                    'data' => $domain.$newImageName,
                    'img' => $domain.$newImageName,
                    'filename' => $newImageName
                ];
                if($type){
                    $result['filename']=$domain.$newImageName;
                }
            }else{
                $savename = [];
                foreach($files as $file) {
                    $savename[] = \think\facade\Filesystem::disk('public')->putFile( 'admin_images', $file);
                }
                $img='/storage/'.str_replace('\\','/',$savename[0]);
                $result = [
                    'code'     => 0,
                    'msg'      => '上传成功',
                    'data' => getFullImageUrl($img),
                    'img' => getFullImageUrl($img),
                    'filename' => $img
                ];
                if($type){
                    $result['filename']=getFullImageUrl($img);
                }
            }
        } catch (\think\exception\ValidateException $e) {
            $result = [
                'code' => -1,
                'msg'  => $e->getMessage()
            ];
        }
        return json($result);exit;
    }
}